<template>
  <div class="app-main">
    <el-card shadow="never">
      <el-row>
        <el-form :inline="true" :model="queryForm" ref="queryForm" label-width="78px">
          <el-form-item label="宠物种类" prop="type">
            <el-select v-model="queryForm.type" clearable placeholder="请选择宠物种类">
              <el-option v-for="item in petTypeArr" :key="item.value" :label="item.label"
                         :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="订单状态" prop="status">
            <el-select v-model="queryForm.status" clearable placeholder="请选择订单状态">
              <el-option v-for="item in orderStatusArr" :key="item.value" :label="item.label"
                         :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="支付状态" prop="payStatus">
            <el-select v-model="queryForm.payStatus" clearable placeholder="请选择订单状态">
              <el-option v-for="item in payStatusArr" :key="item.value" :label="item.label"
                         :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="queryData">查询</el-button>
            <el-button @click="resetForm('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row class="table-container">
        <el-table :data="tableData" v-loading="loading" element-loading-text="数据加载中···" stripe size="medium"
                  style="width: 100%" max-height="660">
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="客户名称" prop="accounts" show-overflow-tooltip></el-table-column>
          <el-table-column label="宠物种类" prop="petId" :formatter="typeRender" show-overflow-tooltip></el-table-column>
          <el-table-column label="服务项目" prop="psid" :formatter="serviceRender" show-overflow-tooltip></el-table-column>
          <el-table-column label="订单金额" prop="price" show-overflow-tooltip></el-table-column>
          <el-table-column label="支付状态" prop="payStatus" :formatter="payStatusRender"
                           show-overflow-tooltip></el-table-column>
          <el-table-column label="提交时间" prop="submitTime" show-overflow-tooltip></el-table-column>
          <el-table-column label="订单状态" prop="status" :formatter="statusRender" show-overflow-tooltip></el-table-column>
          <el-table-column label="接单员工" prop="staffId" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" class="el-icon-circle-check" :disabled="scope.row.status === 1"
                         @click="takeOrder(scope.row)">接单
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.pageNo"
          :page-sizes="page.pageSizes"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import {editOrder, getOrder} from "@/api/order";
import {getServiceList} from "@/api/service";

export default {
  name: "index",
  data() {
    return {
      petTypeArr: [
        {'label': '猫', 'value': '1'},
        {'label': '狗', 'value': '2'},
        {'label': '仓鼠', 'value': '3'},
        {'label': '小猪', 'value': '4'},
        {'label': '兔子', 'value': '5'},
        {'label': '羊驼', 'value': '6'},
        {'label': '鹦鹉', 'value': '7'},
        {'label': '鸽子', 'value': '8'},
        {'label': '乌龟', 'value': '9'},
        {'label': '蝈蝈', 'value': '10'},
        {'label': '金鱼', 'value': '11'},
        {'label': '桑蚕', 'value': '12'},
      ],
      orderStatusArr: [
        {'label': '未接单', 'value': '0'},
        {'label': '已接单', 'value': '1'}
      ],
      payStatusArr: [
        {'label': '未支付', 'value': '0'},
        {'label': '已支付', 'value': '1'}
      ],
      queryForm: {
        type: '',
        status: ''
      },
      loading: false,
      tableData: [],
      page: {
        pageNo: 1,
        pageSizes: [10, 50, 100, 200, 500],
        pageSize: 10,
        total: 10
      },
    }
  },
  created() {
    this.queryPetType()
    this.queryData()
  },
  methods: {
    statusRender(data) {
      for (let i = 0; i < this.orderStatusArr.length; i++) {
        if (data.status === Number(this.orderStatusArr[i].value)) {
          return this.orderStatusArr[i].label
        }
      }
    },
    payStatusRender(data) {
      for (let i = 0; i < this.orderStatusArr.length; i++) {
        if (data.payStatus === Number(this.payStatusArr[i].value)) {
          return this.payStatusArr[i].label
        }
      }
    },
    typeRender(data) {
      for (let i = 0; i < this.petTypeArr.length; i++) {
        if (data.petId === Number(this.petTypeArr[i].value)) {
          return this.petTypeArr[i].label
        }
      }
    },
    serviceRender(data) {
      for (let i = 0; i < this.serviceTypeArr.length; i++) {
        if (data.psid === this.serviceTypeArr[i].id) {
          return this.serviceTypeArr[i].name
        }
      }
    },
    queryPetType() {
      let params = {
        pageNo: 1,
        pageSize: 1000
      }
      getServiceList(params).then(response => {
        if (response.code === 200) {
          this.serviceTypeArr = response.data.list
        } else {
          this.$message.error(response.msg);
        }
      })
    },
    queryData() {
      this.loading = true
      let params = {
        petId: this.queryForm.type,
        status: this.queryForm.status,
        payStatus: this.queryForm.payStatus,
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize
      }
      getOrder(params).then(response => {
        if (response.code === 200) {
          this.tableData = response.data.list
          this.page.total = response.data.total
        } else {
          this.$message.error(response.msg);
        }
        this.loading = false
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.queryData()
    },
    takeOrder(row) {
      this.$confirm(`此单金额为${row.price}元，是否确认接单?`).then(function () {
        return editOrder({
          id: row.id,
          status: 1
        })
      }).then((response) => {
        if (response.code === 200) {
          this.$message({
            message: response.msg,
            type: 'success'
          });
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          });
        }
        this.queryData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消接单'
        });
      })
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.page.pageNo = 1
      this.queryData()
    },
    handleCurrentChange(val) {
      this.page.pageNo = val
      this.queryData()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-main {
  padding: 20px;
  box-sizing: border-box;

  .table-container {
    margin-top: 20px;
  }

  .pagination-container {
    margin: 20px 0;
    float: right;
  }
}
</style>
